import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import global_val from '@/store/global_val'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/Register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/Order/:info',
    name: 'Order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/Admin.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  global_val
})

// 路由守卫
router.beforeEach((to,from,next) => {
  if(global_val.is_admin == false && to.name == 'Admin'){
    alert('您不是管理员')
    next('/')
  } 
  else if(global_val.is_login == false && (to.name == 'Order'||to.name == 'User')){
    alert('请先登录')
    next('/Login')
  }
  else if(to.name == 'Order' && global_val.is_booking==false){
    alert('禁止非法订购')
    next('/')   
  }
  next()
})

router.afterEach((to, from) => {
  if(from.name == 'Order'){
    global_val.is_booking = false
  }
})
export default router